package com.ftg.learn.theory.pattern.myselfmybatis;

import java.awt.*;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.*;
import java.util.*;
import java.util.List;

/**
 * 通用的mybatis 自已封装的
 *
 * @author 布衣
 */
public class MyselfUtil {

    /**
     * 目标对象  关联对象
     * Class c = Class.forName("com.cn.Dog")
     *
     * @param <T>
     * @return
     */
    public static <T> T getInstance(Class<T> t1) {
        return (T) Proxy.newProxyInstance(MyselfUtil.class.getClassLoader(), new Class[]{t1},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                        System.out.println(method.getReturnType().getName() + "方法返回值 类型");
//                        System.out.println(Arrays.toString(method.getParameterTypes()) + "方法的参数类型");
//                        System.out.println(Arrays.toString(method.getAnnotations())+"注解");
                        List<Map<String,Object>> list = new ArrayList<>();
                        Kjx annotation = method.getAnnotation(Kjx.class);
                        if(annotation != null){
                            String sql = annotation.select();
                            //jdbc : java database connectivity
                            String username = "root";
                            String password = "123456";
                            String url = "jdbc:mysql://localhost:3306/qd_db_demo";

                            //加载驱动
                            Class.forName("com.mysql.cj.jdbc.Driver");

                            Connection conn = null;
                            Statement stmt = null;
                            ResultSet rs = null;
                            try{
                                conn = DriverManager.getConnection(url, username, password);
                                stmt = conn.createStatement();

                                rs = stmt.executeQuery(sql);
                                ResultSetMetaData metaData = rs.getMetaData();
                                if(method.getReturnType() == List.class){
                                    while(rs.next()){
                                        Map<String,Object> map = new HashMap<>();
                                        for(int i = 0;i < metaData.getColumnCount();i++){
                                            map.put(metaData.getColumnName(i+1),rs.getObject(i+1));
                                        }
                                        list.add(map);
                                    }
                                }
                            }catch (Exception e){
                                e.printStackTrace();
                            }finally {
                                try{
                                    if(rs != null){
                                        rs.close();
                                    }
                                    if(stmt != null){
                                        stmt.close();
                                    }
                                    if(conn != null){
                                        conn.close();
                                    }
                                }catch (Exception e){
                                    e.printStackTrace();
                                }
                            }

                        }
                        return list;
                    }
                });
    }

    ;

    public static void main(String[] args) {
        TeacherMapper t = MyselfUtil.getInstance(TeacherMapper.class);
        System.out.println(t.showall2());
    }

}
